package com.zaoniao.enstudy.infrastructure.persistence;

import com.zaoniao.enstudy.domain.entity.ZaoWordExample;
import com.zaoniao.enstudy.domain.repository.ZaoWordExampleRepository;
import com.zaoniao.enstudy.infrastructure.convert.ZaoWordExampleCrt;
import com.zaoniao.enstudy.infrastructure.mapper.ZaoWordExampleMapper;
import com.zaoniao.enstudy.infrastructure.po.ZaoWordExamplePo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author llb666
 */
@Repository
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ZaoWordExampleRepositoryImpl implements ZaoWordExampleRepository {

    private final ZaoWordExampleMapper zaoWordExampleMapper;

    /**
     * 根据单词查询其相关联的例句.
     *
     * @param word 单词
     * @return 例句列表
     */
    @Override
    public List<ZaoWordExample> getExamples(String word) {
        Example example = new Example(ZaoWordExamplePo.class);
        example.orderBy("id").desc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("word", word);
        return ZaoWordExampleCrt.INSTANCE.po2doList(zaoWordExampleMapper.selectByExample(example));
    }

    /**
     * 批量新增单词例句
     *
     * @param zaoWordsEgOne -
     */
    @Override
    public void batchInsert(List<ZaoWordExample> zaoWordsEgOne) {
        zaoWordExampleMapper.insertList(ZaoWordExampleCrt.INSTANCE.do2poList(zaoWordsEgOne));
    }
}
